package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.lowcode.LowCodeActionSettingService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.actionsetting.pojo.dto.LowCodeActionSettingAddDTO;
import com.xbongbong.pro.actionsetting.pojo.dto.LowCodeActionSettingGetDTO;
import com.xbongbong.pro.actionsetting.pojo.dto.LowCodeActionSettingListDTO;
import com.xbongbong.pro.actionsetting.pojo.dto.LowCodeFieldActionGetDTO;
import com.xbongbong.pro.actionsetting.pojo.vo.LowCodeActionSettingAddVO;
import com.xbongbong.pro.actionsetting.pojo.vo.LowCodeActionSettingGetVO;
import com.xbongbong.pro.actionsetting.pojo.vo.LowCodeActionSettingListVO;
import com.xbongbong.pro.actionsetting.pojo.vo.LowCodeFieldActionGetVO;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/03/03 17:18
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/actionSet")
public class LowCodeActionSettingController {


    @Resource
    private LowCodeActionSettingService lowCodeActionSettingService;

    /**
     * 列表
     *
     * @param lowCodeFieldActionGetDTO
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid LowCodeFieldActionGetDTO lowCodeFieldActionGetDTO, BindingResult br) throws Exception {
        XbbResponse<LowCodeFieldActionGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LowCodeFieldActionGetVO serviceRegistrationListVO = lowCodeActionSettingService.getFieldActionList(lowCodeFieldActionGetDTO);
                response = new XbbResponse<>(serviceRegistrationListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * list
     *
     */
    @RequestMapping(value = "/fieldList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid LowCodeActionSettingListDTO lowCodeActionSettingListDTO, BindingResult br) throws Exception {
        XbbResponse<LowCodeActionSettingListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LowCodeActionSettingListVO lowCodeActionSettingListVO = lowCodeActionSettingService.fieldList(lowCodeActionSettingListDTO);
                response = new XbbResponse<>(lowCodeActionSettingListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
    /**
     * get
     *
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String get(@RequestBody @Valid LowCodeActionSettingGetDTO lowCodeActionSettingGetDTO, BindingResult br) throws Exception {
        XbbResponse<LowCodeActionSettingGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LowCodeActionSettingGetVO lowCodeActionSettingGetVO = lowCodeActionSettingService.get(lowCodeActionSettingGetDTO);
                response = new XbbResponse<>(lowCodeActionSettingGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 新建
     *
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String add(@RequestBody @Valid LowCodeActionSettingAddDTO lowCodeActionSettingAddDTO, BindingResult br) throws Exception {
        XbbResponse<LowCodeActionSettingAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LowCodeActionSettingAddVO lowCodeActionSettingAddVO = lowCodeActionSettingService.add(lowCodeActionSettingAddDTO);
                response = new XbbResponse<>(lowCodeActionSettingAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
//
//    /**
//     * 更新
//     *
//     */
//    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    public String update(@RequestBody @Valid ServiceRegistrationUpdateDTO serviceRegistrationUpdateDTO, BindingResult br) throws Exception {
//        XbbResponse<ServiceRegistrationUpdateVO> response;
//        if (br.hasErrors()) {
//            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
//            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
//        } else {
//            try {
//                ServiceRegistrationUpdateVO serviceRegistrationUpdateVO = serviceRegistrationService.update(serviceRegistrationUpdateDTO);
//                response = new XbbResponse<>(serviceRegistrationUpdateVO);
//            } catch (XbbException e) {
//                response = new XbbResponse<>(e);
//            }
//        }
//        return JSON.toJSONString(response);
//    }
//
//    /**
//     * 删除
//     *
//     */
//    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    public String delete(@RequestBody @Valid ServiceRegistrationDeleteDTO serviceRegistrationDeleteDTO, BindingResult br) throws Exception {
//        XbbResponse<ServiceRegistrationDeleteVO> response;
//        if (br.hasErrors()) {
//            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
//            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
//        } else {
//            try {
//                ServiceRegistrationDeleteVO serviceRegistrationDeleteVO = serviceRegistrationService.delete(serviceRegistrationDeleteDTO);
//                response = new XbbResponse<>(serviceRegistrationDeleteVO);
//            } catch (XbbException e) {
//                response = new XbbResponse<>(e);
//            }
//        }
//        return JSON.toJSONString(response);
//    }

}
